package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketNoticeAdmin;
import com.cskaoyan.bean.PO.MarketNoticeAdminExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketNoticeAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wenkaikai
 * @since 2022/08/15 11:09
 */
@Service
@Transactional
public class AdminProfileServiceImpl implements AdminProfileService {

    @Autowired
    MarketNoticeAdminMapper marketNoticeAdminMapper;


    /**
     * @param info
     * @param title 模糊查询 标题
     * @param type  分为未读（unread）、 已读（read）、所有（all）
     * @return
     */
    @Override
    public CommonData<MarketNoticeAdmin> queryNoticeList(BasePageInfo info, String title, String type) {
        MarketNoticeAdminExample example = new MarketNoticeAdminExample();
        // 都不为空，拼接上order by 条件
        if (info.getSort() != null && info.getOrder() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketNoticeAdminExample.Criteria criteria = example.createCriteria();
        // 不为空，拼接上查询条件
        if (title != null && !"".equals(title)) {
            criteria.andNoticeTitleLike("%" + title + "%");
        }
        // type 不会为空，// all的话不用拼接条件
        if ("read".equals(type)) {
            // 已读的话，readTime不能为空
            criteria.andReadTimeIsNotNull();
        } else if ("unread".equals(type)) {
            // 未读时查询为空的readTime数据
            criteria.andReadTimeIsNull();
        }
        // 查询前开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        List<MarketNoticeAdmin> marketNoticeAdmins = marketNoticeAdminMapper.selectByExample(example);
        // 查询结果交给分页工具处理
        PageInfo<MarketNoticeAdmin> marketNoticeAdminPageInfo = new PageInfo<>(marketNoticeAdmins);
        CommonData data = CommonData.data(marketNoticeAdminPageInfo);
        return data;
    }
}